package org.zhigong.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.zhigong.dto.SysChuqin;
import org.zhigong.dto.SysResult;
import org.zhigong.dto.SysSearch;
import org.zhigong.entity.Chuqin;
import org.zhigong.entity.Page;
import org.zhigong.service.ChuqinService;

import java.util.Date;
import java.util.List;

import static org.zhigong.util.CommonUtil.isEmpty;
import static org.zhigong.util.CommonUtil.isNumeric;


@Controller
public class ChuqinController {

    @Autowired
    private ChuqinService chuqinService;

    // 只做跳转用,初始化使用json获取数据处理.
    @RequestMapping(value = "/chuqins", method = RequestMethod.GET)
    public String init(){
        return "chuqin";
    }

    // 分页查询获取列表,返回json
    @RequestMapping(
            value = "/chuqins",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<List<Chuqin>> search(SysChuqin sysChuqin){
        SysResult<List<Chuqin>> sysResult;

        Chuqin chuqin = new Chuqin();
        String condS = sysChuqin.getCondition();
        chuqin.setXm(condS);
        chuqin.setKsrq(sysChuqin.getKsrq());
        chuqin.setJsrq(sysChuqin.getJsrq());

        chuqin.getPage().setCurrentPage(sysChuqin.getCurrentPage());
        chuqin.getPage().setPageNumber(sysChuqin.getPageNumber());

        try{
            List<Chuqin> chuqinList = chuqinService.getChuqinList(chuqin);
            if(chuqinList.size() > 0){
                chuqinList.get(0).setPage(chuqin.getPage());
            }
            sysResult = new SysResult<List<Chuqin>>(true, chuqinList);
        }catch (Exception e){
            sysResult = new SysResult<List<Chuqin>>(false, e.getMessage());
        }

        return sysResult;
    }

    // 新增
    @RequestMapping(
            value = "/chuqins",
            method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> create(@RequestBody Chuqin chuqin){
        SysResult<Integer> sysResult;
        try{
            Boolean add  = chuqinService.addChuqin(chuqin);
            sysResult = new SysResult<Integer>(true, add ? 1 : 0);
        }catch (Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }


    // 删除
    @RequestMapping(
            value = "/chuqins/{ygbh}/{rq}",
            method = RequestMethod.DELETE,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> delete(@PathVariable("ygbh") Long ygbh, @PathVariable("rq") Date rq){
        SysResult<Integer> sysResult;
        Chuqin chuqin = new Chuqin();
        chuqin.setYgbh(ygbh);
        chuqin.setRq(rq);
        try{
            Boolean del = chuqinService.deleteChuqin(chuqin);
            sysResult = new SysResult<Integer>(true, del ? 1 : 0);
        }catch(Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }

    // 更新
    @RequestMapping(
            value = "/chuqins/{ygbh}/{rq}",
            method = RequestMethod.PUT,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> update(@RequestBody Chuqin chuqin){
        SysResult<Integer> sysResult;
        try{
            Boolean upd = chuqinService.updateChuqin(chuqin);
            sysResult = new SysResult<Integer>(true, upd ? 1 : 0);
        }catch(Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }

    // 查看
    @RequestMapping(
            value = "/chuqins/{ygbh}/{rq}",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Chuqin> view(@PathVariable("ygbh") Long ygbh, @PathVariable("rq") Date rq){
        SysResult<Chuqin> sysResult;
        Chuqin chuqin = new Chuqin();
        chuqin.setYgbh(ygbh);
        chuqin.setRq(rq);
        try{
            Chuqin chuqinNew = chuqinService.getOndDay(chuqin);
            sysResult = new SysResult<Chuqin>(true, chuqinNew);
        }catch(Exception e){
            sysResult = new SysResult<Chuqin>(false, e.getMessage());
        }
        return sysResult;
    }
}
